/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using Shuriken.Core.Ranking;
using Shuriken.Base;

namespace Shuriken.Core.Store
{


    /// <summary>
    /// Attribute used for decorating the action with its name 
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, Inherited= true, AllowMultiple=false)]
    public class ActionNameAttribute : Attribute
    {
        protected string m_actionName; 

        public ActionNameAttribute(string actionName)
        {
            m_actionName = actionName;
        }

        /// <summary>
        /// Name of the action 
        /// </summary>
        public string ActionName
        {
            get
            {
                return m_actionName;
            }
        }
    }

    /// <summary>
    /// Attribute used for decorating the action with its subject type
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
    public class ActionSubjectTypeAttribute : Attribute
    {
        protected Type m_subject;

        public ActionSubjectTypeAttribute(Type subject)
        {
            m_subject = subject; 
        }

        /// <summary>
        /// Subject type for this action
        /// </summary>
        public Type ActionSubject
        {
            get
            {
                return m_subject; 
            }
        }
    }

    [AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
    public class DefaultActionAttribute : Attribute { }

    /// <summary>
    /// Base class for all action
    /// </summary>
    public abstract class BaseAction : IAction, ISearchable
    {
        private double m_Rank;
        private MutableIndexSet m_HitMask;
        private string m_SearchTerm; 
        protected bool m_IsAttributed;
        private string m_Attribute; 

        protected static string IMAGE_NOT_LOADED = string.Empty;
        protected static string DEFAULT_IMAGE = string.Empty; 

        static BaseAction()
        {
            // Load the default image
            DEFAULT_IMAGE = ImageStore.GetDefaultActionImage();
        }

        public BaseAction()
        {
            m_IsAttributed = false;
            m_Attribute = string.Empty;
            m_HitMask = new MutableIndexSet();
            m_SearchTerm = string.Empty; 
        }

        /// <summary>
        /// Registers action with the class repsonsible for managing actions
        /// </summary>
        public virtual void RegisterAction()
        {
            ActionManager.GetDefaultInstance().RegisterAction(SubjectType, this);
        }

        /// <summary>
        /// Retrieves the ActionName attribute value 
        /// </summary>
        /// <returns>name of the action</returns>
        protected virtual string RetrieveActionNameAttribute()
        {
            ActionNameAttribute attrActionName = (ActionNameAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(ActionNameAttribute));

            if (null == attrActionName)
            {
                throw new Exception("Name attribute is not defined for this action!");
            }
            else
            {
                return attrActionName.ActionName; 
            }
        }

        /// <summary>
        /// Retrieves the type of the subject that this action acts on. 
        /// </summary>
        /// <returns>type of subject</returns>
        protected virtual Type RetrieveSubjectTypeAttribute()
        {
            ActionSubjectTypeAttribute attrSubjectType = (ActionSubjectTypeAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(ActionSubjectTypeAttribute));

            if (null == attrSubjectType)
            {
                throw new Exception("Subject type is not defined for this action!");
            }
            else
            {
                return attrSubjectType.ActionSubject;
            }
        }

        /// <summary>
        /// Retrieves the vlaue of DefaultActionAttribute 
        /// </summary>
        /// <returns>true, if this action is default for its subject</returns>
        protected virtual bool RetrieveDefaultAttribute()
        {
            DefaultActionAttribute attrDefaultAction = (DefaultActionAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(DefaultActionAttribute));

            if (null == attrDefaultAction)
            {
                return false;
            }
            else
            {
                return true; 
            }
        }

        #region IAction Members

        /// <summary>
        /// Executing the action
        /// </summary>
        public abstract void Execute(Shuriken.Base.ISubject subject);

        public abstract bool CanExecuteOn(ISubject subject);

        /// <summary>
        /// Name for the action
        /// </summary>
        public string Name
        {
            get
            {
                return RetrieveActionNameAttribute();
            }
        }

        /// <summary>
        /// Retrieves the type of subject this action is executed on
        /// </summary>
        public Type SubjectType
        {
            get
            {
                return RetrieveSubjectTypeAttribute();
            }
        }

        /// <summary>
        /// Default Action
        /// </summary>
        public bool IsDefault
        {
            get
            {
                return RetrieveDefaultAttribute();
            }
        }

        /// <summary>
        /// Attribute used for parametering an action
        /// </summary>
        /// <see cref="IAction"/>
        public string ActionAttribute
        {
            get
            {
                return m_Attribute; 
            }
            set
            {
                m_Attribute = value; 
            }
        }

        /// <summary>
        /// Returns false as this class has no attributes by default. 
        /// </summary>
        public bool IsAttributed
        {
            get
            {
                return m_IsAttributed;
            }
        }

        ///// <summary>
        ///// Returns the display name of the action 
        ///// </summary>
        //public string Name
        //{
        //    get
        //    {
        //        return this.Name; 
        //    }
        //}

        /// <summary>
        /// Returns the display description of the action 
        /// </summary>
        public string Description
        {
            get
            {
                return string.Empty; 
            }
        }

        /// <summary>
        /// Returns the image index for the icon used for the action 
        /// </summary>
        public string Icon
        {
            get
            {
                return DEFAULT_IMAGE; 
            }
            set
            {
            }
        }

        /// <summary>
        /// Rank for this action 
        /// </summary>
        public double Rank
        {
            get
            {
                return m_Rank; 
            }
            set
            {
                m_Rank = value; 
            }
        }

        public MutableIndexSet HitMask
        {
            get { return m_HitMask; }
            set { m_HitMask = value; }
        }

        /// <summary>
        /// Last search term used to select this item 
        /// </summary>
        public string SearchTerm
        {
            get
            {
                return m_SearchTerm;
            }
            set
            {
                m_SearchTerm = value;
            }
        }

        /// <summary>
        /// Unique id
        /// </summary>
        public string UniqueIdentifier
        {
            get
            {
                return string.Format("Action.{0}", this.Name);
            }
        }

        #endregion
    }
}
